class DailyInformationBean {
  List<Data>? data;
  String? msg;
  int? status;

  DailyInformationBean({this.data, this.msg, this.status});

  DailyInformationBean.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(Data.fromJson(v));
      });
    }
    msg = json['msg'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    data['msg'] = this.msg;
    data['status'] = this.status;
    return data;
  }
}

class Data {
  String? bGNAME;
  dynamic? bLAPPLYDATE;
  int? bLBILLNUM;
  int? bLCOST;
  dynamic? bLPCOST;
  String? bLVALIDITY;
  String? cMADDRESS;
  String? cMCODE;
  String? cMNAME;

  Data(
      {this.bGNAME,
        this.bLAPPLYDATE,
        this.bLBILLNUM,
        this.bLCOST,
        this.bLPCOST,
        this.bLVALIDITY,
        this.cMADDRESS,
        this.cMCODE,
        this.cMNAME});

  Data.fromJson(Map<String, dynamic> json) {
    // bGNAME = json['BG_NAME'];
    var bgNameTemp = json['BG_NAME'];
    bGNAME = getGroupName(json['CM_CODE'], bgNameTemp);
    // bLAPPLYDATE = json['BL_APPLYDATE'];
    // 时间戳转日期格式
    bLAPPLYDATE = timestampToDate(json['BL_APPLYDATE']);
    bLBILLNUM = json['BL_BILLNUM'];
    bLCOST = json['BL_COST'];
    bLPCOST = json['BL_PCOST'];
    bLVALIDITY = json['BL_VALIDITY']??"一年";
    // cMADDRESS = json['CM_ADDRESS'];
    cMADDRESS = adjustAddresses(json['CM_ADDRESS']);
    cMCODE = json['CM_CODE'];
    cMNAME = json['CM_NAME'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['BG_NAME'] = this.bGNAME;
    data['BL_APPLYDATE'] = this.bLAPPLYDATE;
    data['BL_BILLNUM'] = this.bLBILLNUM;
    data['BL_COST'] = this.bLCOST;
    data['BL_PCOST'] = this.bLPCOST;
    data['BL_VALIDITY'] = this.bLVALIDITY;
    data['CM_ADDRESS'] = this.cMADDRESS;
    data['CM_CODE'] = this.cMCODE;
    data['CM_NAME'] = this.cMNAME;
    return data;
  }

  // 返回单个群组名称
  String getGroupName(String customerId, String groupName) {
    String? newGroupName;
    if (groupName == "EASY" || groupName == "WINSON" || groupName == "FDC") {
      newGroupName = groupName;
    } else if (customerId == "96571V") {
      newGroupName = "CXC-A組";
    } else if ((customerId == "13192011" && groupName == "CXC EXPRESS") || customerId == "CXC EXPRESS") {
      newGroupName = "CXC-D組";
    } else if (groupName == "ACS") {
      newGroupName = "CXC-信誠";
    } else if (groupName == "CITY") {
      newGroupName = "CXC-城市";
    } else if (groupName == "ASIAN") {
      newGroupName = "亞洲";
    } else if (groupName == "FC") {
      newGroupName = "飞鹏";
    } else if (groupName == "CMS" ||
        groupName == "EDS" ||
        groupName == "JUMBO" ||
        groupName == "PT" ||
        groupName == "CX COURIER" ||
        groupName == "CXC EXPRESS (新)" ||
        groupName == "DDS" ||
        groupName == "FAST DELIVERY") {
      if(customerId[0] == "A" || customerId[0] == "B" || customerId[0] == "C" || customerId[0] == "D"){
        newGroupName = "CXC-${customerId[0]}組";
      }else{
        newGroupName = "CXC-D組";
      }
    } else if (groupName.contains("CXC")) {
      newGroupName = "CXC-${customerId[0]}組";
    } else {
      newGroupName = groupName;
    }
    return newGroupName;
  }

    String adjustAddresses(String input) {

      // 换行的数据调整为1行显示，他们是用\n分割的,可以直接把\n 换成 \r
      input = input.replaceAll("\n", " ");
      input = input.replaceAll("\r", " ");

  //   List<String> lines = input.split('\n');
  //   StringBuffer adjustedAddresses = StringBuffer();
  //   bool inQuotes = false;
  //   StringBuffer currentAddress = StringBuffer();
  //   for (String line in lines) {
  //     line = line.trim();
  //     if (line.startsWith('"') && !inQuotes) {
  //       // 开始新的引号部分
  //       inQuotes = true;
  //       currentAddress.write(line.substring(1)); // 去掉开头的引号
  //     } else if (line.endsWith('"') && inQuotes) {
  //       // 结束引号部分
  //       inQuotes = false;
  //       currentAddress.write(' ${line.substring(0, line.length - 1)}'); // 去掉结尾的引号
  //       adjustedAddresses.writeln(currentAddress.toString().trim()); // 添加整合后的地址
  //       currentAddress.clear(); // 清空当前地址
  //     } else if (inQuotes) {
  //       // 在引号内的行，继续添加
  //       currentAddress.write(' $line');
  //     } else {
  //       // 不在引号内，直接添加
  //       adjustedAddresses.writeln(line);
  //     }
  //   }
  //   return adjustedAddresses.toString().trimRight();
      return input;
  }

  // 时间戳转日期格式
  String timestampToDate(int timestamp) {

    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    print("${dateTime}");
    // 月日 时分秒 保留两位数
    String formatDate = "${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}:${dateTime.second.toString().padLeft(2, '0')}";
    return formatDate;
  }


}
